/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        vector<pair<TreeNode*,unsigned long long>> v;
        unsigned long long ret=1;
        v.push_back({root,1});

        while(v.size())
        {
            int sz=v.size();
            vector<pair<TreeNode*,unsigned long long>> tmp;
            for(int i=0;i<v.size();i++)
            {
                auto t=v[i];

                if(t.first->left)
                {
                    tmp.push_back({t.first->left,t.second*2});
                }
                if(t.first->right)
                {
                    tmp.push_back({t.first->right,t.second*2+1});
                }
               
            }
            if(tmp.size())
                ret=max(ret,tmp.back().second-tmp[0].second+1);
            v=tmp;
        }
        return ret;
    }
};
